﻿using System.Drawing;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;

namespace c2___c3
{
    internal class Program
    {
        interface IShape
        {
            bool isVaild();
            double getArea();
        }

        abstract public class Shape : IShape
        {
            public abstract double getArea();
            public abstract bool isVaild();
        }
         
        //长方形
        class Rectangle : Shape 
        {
            public int width { get; set; }
            public int height { get; set; }

            public Rectangle(int width, int height)
            {
                this.width = width;
                this.height = height;
            }

            public override double getArea()
            {
                if (isVaild())
                {
                    return width * height;
                }
                else throw new InvalidOperationException("长方形不存在");
            }

            public override bool isVaild()
            {
                return (width > 0) && (height > 0);
            }
        }

        class Triangle : Shape
        {
            public int a { get; set; }
            public int b { get; set; }
            public int c { get; set; }

            public Triangle(int a, int b, int c) 
            {
                this.a = a; this.b = b; this.c = c;
            }

            public override bool isVaild()
            {
                return (a > 0) && (b > 0) && (c > 0) && (a + b > c) && (b + c > a) && (a + c > b);
            }

            public override double getArea()
            {
                if (isVaild())
                {
                    double s = (a + b + c) / 2;
                    return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
                }
                else throw new InvalidOperationException("三角形不存在");
            }

        }

        class Square : Shape
        {
            public int side { get; set; }
            public Square(int side) { this.side = side; }

            public override bool isVaild() { return side > 0; }

            public override double getArea()
            {
                if (isVaild()) return side * side;
                else throw new InvalidOperationException("正方形不存在");
            }
        }

        public class ShapeFactory
        {
            //随机生成
            private static readonly Random random = new Random();

            public static Shape RandomCreateShape()
            {
                int shapetype = random.Next(3);

                switch (shapetype)
                {
                    case 0:
                        return new Square(random.Next(1,10));
                    case 1:
                        return new Rectangle(random.Next(1, 10), random.Next(1, 10));
                    case 2:
                        return new Triangle(random.Next(1,10), random.Next(1, 10),random.Next(1,10));
                    default:
                        throw new InvalidOperationException("图形不存在");
                }
            }
        }
        static void Main(string[] args)
        {
            Shape rect = new Rectangle (5, 10);
            Console.WriteLine($"长方形面积: {rect.getArea()}");  // 50

            Shape square = new Square(4);
            Console.WriteLine($"正方形面积: {square.getArea()}");  // 16

            Shape triangle = new Triangle(3, 4, 5);
            Console.WriteLine($"三角形面积: {triangle.getArea()}");  // 6

            // 非法情况测试
            try
            {
                Shape invalidTriangle = new Triangle(1, 1, 3);
                Console.WriteLine(invalidTriangle.getArea());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);  // 输出 "非法三角形"
            }

            //形状数组
            Shape[] shapes = new Shape[10];
            double sum = 0;
            
            for(int i =0;i<10;i++)
            {
                shapes[i] = ShapeFactory.RandomCreateShape();
                while( !shapes[i].isVaild() )
                {
                    shapes[i] = ShapeFactory.RandomCreateShape();
                }
                sum += shapes[i].getArea();

                Console.WriteLine($"形状 {i + 1}: {shapes[i].GetType().Name}, 面积: {shapes[i].getArea()}");
            }

            Console.WriteLine("面积和为"+sum);
        }
    }
}
